<?php

class ocorrencia_configuracoesModel extends Object {

    private $type = "";
    public function __construct() {
        $this->LoadModel("usuario/login", 'uobj');
        if($this->uobj->UserIsWebmaster()) $this->setConfigType("ConfigAdmin");
        else $this->setConfigType("Config");
    }
    
    public function setConfigType($conf){
        $arr = array("ConfigAdmin", "Config");
        $this->type = in_array($conf, $arr)?$conf:"Config";
        
    }
   
    private function filtherconfigfiles($files){
        $out = array();
        foreach($files as $file){
                $temp = str_replace(array("dicionario.", ".php"), "", $file);
                $out[$temp] = str_replace(array('_', '-'), ' ', $temp);
        }
        return $out;
    }
    public function selecionar(){
        
        //carrega as configuracoes gerais
        $this->LoadResource("files/dir", 'dir');
        $files = $this->dir->getArquivos(MODULOS . "/ocorrencia/Config/defines/");
        $out["Configurações"] = $this->filtherconfigfiles($files);
        return ($out);
    }
    
    private function getFileName($file){
        $dir  = MODULOS . "ocorrencia/Config/defines/dicionario.$file.php";
        
        if(!file_exists($dir)){
            $dir = MODULOS . "ocorrencia/Config/defines/$file.php";
        }
        return $dir;
    }

    public function hasConfigurationFile($file){
        $arq = $this->getFileName($file);
        return file_exists($arq);
    }

    public function getConf($file){
        if(!$this->loadObj($file, 'obj')){
            return array();
        }
        return $this->obj->select();
    }

    public function editar($conffile, $dados){
        $this->loadObj($conffile, 'obj');
        $bool = $this->obj->editar($dados);
        $this->setMessages($this->obj->getMessages());
        return $bool;
    }

    private function loadObj($file, $obj){
        if(!$this->hasConfigurationFile($file)){
            $this->setErrorMessage("o arquivo $file não existe");
            return false;
        }
        $arq = $this->getFileName($file);
        require_once $arq;
               
        $this->$obj = new configModel();
   
        $class = explode("/", $arq);
        $class = end($class);
        $class = str_replace('.php', '', $class);
        $this->$obj->setFilename($arq, $class);
        return true;
    }
    
    public function getModelName() {
        return "";
    }
    public function getModelLabel(){
    	return "";
    }
    
    public function getModelDescription(){
    	return "";
    }

}

?>
